#ifndef _CHIP_UART_HPP_
#define _CHIP_UART_HPP_

#include <string>
#include <vector>
extern "C"
{
#include <rtthread.h>
#include <rtdevice.h>
#include <stdint.h>
}

typedef enum
{
    chip_uart_status_ok,
    chip_uart_status_fail,
} chip_uart_status;

class chip_uart_message
{
public:
    uint8_t* message;
    uint32_t message_length;
    uint32_t now_end_pos;

    chip_uart_message();
    chip_uart_message(uint32_t length);
    chip_uart_message(const chip_uart_message &ob);
    ~chip_uart_message();

    void add_uin8_t(uint8_t add_data);
};

class CHIP_UART
{
private:
    std::vector<chip_uart_message*> receive_list;
    rt_device_t serial;
    rt_sem_t serial_sem;
    std::string uart_name;
    struct serial_configure serial_config;

    chip_uart_status init_flag;

public:
    CHIP_UART(std::string uart_name, struct serial_configure* serial_config);
    ~CHIP_UART();
    chip_uart_status CHIP_UART_read_init_status();
    chip_uart_status CHIP_UART_init_rx_interrupt(rt_err_t (*rxfunc)(rt_device_t dev, rt_size_t size));

    void CHIP_UART_rx_default_callback(); //外部定义rxfunc在其中调用该函数，并返回RT_EOK，将定义好的函数放入CHIP_UART_init_rx_interrupt中
    void CHIP_UART_rx_default_thread_handler(); //外部定义接收线程在其中调用该函数，并启动线程

    chip_uart_status CHIP_UART_send(uint32_t send_len,const void* send_buff);
    chip_uart_message* CHIP_UART_read_receive(uint32_t timeout); //通过返回值读出数据，使用完成后需要delete，timeout设置读取超时时间ms

};

#endif /* _CHIP_UART_HPP_ */
